const { parentPort } = require("worker_threads");

const { url } = require("../config/ncep.config");
const { CreatePath, MergeUrl, ParserUrl } = require("../utils");
parentPort.on("message", async ({ eleKey, startTime, levels, times }) => {
  try {
    const baseDir = CreatePath(eleKey, startTime, levels, times);
    const urls = MergeUrl(
      eleKey,
      levels,
      times,
      url.interval,
      startTime,
      url.startAging,
      url.endAging
    );

    const parser = ParserUrl({
      baseDir,
      baseUrl: url.dir,
      file_suffix: url.file_suffix,
    });
    await parser(urls);

    parentPort.postMessage({
      status: 200,
      data: "保存数据成功",
    });
  } catch (error) {
    parentPort.postMessage({
      status: 404,
      data: error,
    });
  }
});
